package com.slz3.business.frame.conf;

import com.slz3.business.frame.redis.RK;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

import java.util.ArrayList;
import java.util.List;

@Configuration
@AutoConfigureAfter({Receiver.class})
public class SubscriberConfig {
    /**
     * 消息监听适配器，注入接受消息方法，输入方法名字 反射方法
     *
     * @param receiver
     * @return
     */
    @Bean
    public MessageListenerAdapter getMessageListenerAdapter(Receiver receiver) {
        //当没有继承MessageListener时需要写方法名字
        return new MessageListenerAdapter(receiver, "receiveMessage");
    }

    /**
     * 创建消息监听容器
     *
     * @param redisConnectionFactory
     * @param messageListenerAdapter
     * @return
     */
    @Bean
    public RedisMessageListenerContainer getRedisMessageListenerContainer(RedisConnectionFactory redisConnectionFactory, MessageListenerAdapter messageListenerAdapter) {
        RedisMessageListenerContainer redisMessageListenerContainer = new RedisMessageListenerContainer();
        redisMessageListenerContainer.setConnectionFactory(redisConnectionFactory);

        //监听的list
        List<PatternTopic> list = new ArrayList();
        //注册
        list.add(new PatternTopic(RK.USER_USERREGIST));
        //添加购物车
        list.add(new PatternTopic(RK.USER_SHOPPTING_ADDCAR));
        //购物车删除
        list.add(new PatternTopic(RK.SHOPPTING_CAR_DELETE));
        //购物车修改
        list.add(new PatternTopic(RK.SHOPPTING_CAR_MODIFY));
        //下订单
        list.add(new PatternTopic(RK.SAVE_ORDER));
        //订单删除
        list.add(new PatternTopic(RK.ORDER_DELETE));
        //订单取消
        list.add(new PatternTopic(RK.CLOSE_ORDER));
        //确认收货
        list.add(new PatternTopic(RK.CONFIRM_RECEIVER));
        //积分支付
        list.add(new PatternTopic(RK.JIFEN_PAY));
        //微信支付
        list.add(new PatternTopic(RK.APPPAY_WEIXINPAYUPDATE));
        //支付宝支付
        list.add(new PatternTopic(RK.APPPAY_ALIPAYUPDATE));
        //售后退款
        list.add(new PatternTopic(RK.ORDERRETURN_APPLYAFTERSALELIST));
        //撤销售后退款
        list.add(new PatternTopic(RK.ORDERRETURN_RECALLREFUNDLIST));
        //售后用户发货
        list.add(new PatternTopic(RK.ORDERRETURN_DELIVERLIST));
        //意见反馈
        list.add(new PatternTopic(RK.FEED_BACK_LIST));
        //积分
        list.add(new PatternTopic(RK.JI_FEN_RECORD));
        //抽奖券
        list.add(new PatternTopic(RK.DRAW_TICKET_LIST));
        //第三方用户同步
        list.add(new PatternTopic(RK.User_THIRD));

        /* 通配监听 监听多个队列*/
        redisMessageListenerContainer.addMessageListener(messageListenerAdapter, list);
        return redisMessageListenerContainer;
    }

}
